package com.lsw.photoview;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.lsw.Base.BaseHolder;
import com.lsw.Base.BaseSimpleAdapter;
import com.lsw.R;
import com.lsw.config.CacheManager;
import com.lsw.photo.IBanner;
import com.lsw.photo.PublicImage;
import com.lsw.util.DimenUtils;
import com.lsw.util.LogUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 个人中心宣称册图片列表适配器
 *
 * @author lsw
 */
public class AddImageAdapter extends BaseSimpleAdapter<PublicImage> {
    private int requestCode;
    private boolean Figure;
    private int key_manyPhotoMaxCount = -1;

    public AddImageAdapter(Context context, int requestCode, boolean Figure) {
        super(context);
        initImage();
        this.requestCode = requestCode;
        this.Figure = Figure;
    }

    public int getKey_manyPhotoMaxCount() {
        return key_manyPhotoMaxCount;
    }

    public void setKey_manyPhotoMaxCount(int key_manyPhotoMaxCount) {
        this.key_manyPhotoMaxCount = key_manyPhotoMaxCount;
    }

    private int width_Img, height_Img;

    /**
     * 动态计算图片宽高，左右边距为5，图片左右边距为5
     */
    private void initImage() {
        width_Img = (DimenUtils.getScreenSize((Activity) context)[0] - DimenUtils.dip2px(context, 50)) / 4;
//        height_Img = (int) (width_Img * 0.635f);
        height_Img = width_Img;
    }

    @Override
    protected BaseHolder<PublicImage> getHolder() {
        return new BaseHolder<PublicImage>() {
            ImageView img, iv_delect;

            @Override
            public void bindViews(View parentView) {
                img = (ImageView) parentView.findViewById(R.id.img);
                iv_delect = (ImageView) parentView.findViewById(R.id.iv_delect);
                LayoutParams params = img.getLayoutParams();
                if (params != null) {
                    params.width = width_Img;
                    params.height = height_Img;
                }
            }

            @Override
            public void bindData(final PublicImage item, final int position) {
                if (position == 0 && item.isAdd) {
                    img.setScaleType(ScaleType.CENTER_INSIDE);
                    img.setImageResource(R.mipmap.carma);
                } else {
                    img.setScaleType(ScaleType.CENTER_CROP);

                    if (!TextUtils.isEmpty(item.cover)) {
                        if (item.cover.startsWith("http://")) {
                            // 网络图片
                            ImageLoader.getInstance().displayImage(item.cover,
                                    img, CacheManager.getDefaultDisplay());
                        } else {
                            // 本地图片
                            ImageLoader.getInstance().displayImage(
                                    Uri.fromFile(new File(item.cover))
                                            .toString(),
                                    img,
                                    CacheManager
                                            .getLocalDisplayerOptions4Upload());
                        }
                    } else if (item.getResource() > 0) {
                        // 加载的是图片资源
                        ImageLoader.getInstance().displayImage(
                                "drawable://" + item.getResource(), img,
                                CacheManager.getLocalDisplayerOptions4Upload());
                    }
                }

                iv_delect.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (adapterRefresh != null)
                            adapterRefresh.onRefreshAdapter(position, -1);
                    }
                });
                if (position == 0 && item.isAdd) {
                    iv_delect.setVisibility(View.INVISIBLE);
                } else {
                    iv_delect.setVisibility(View.VISIBLE);
                }
                img.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (position == 0 && item.isAdd) {
                            if (Figure && key_manyPhotoMaxCount != -1) {
                                TakePhotoActivity.startActivityForSiglePhoto(
                                        (Activity) context, requestCode, false, Figure, key_manyPhotoMaxCount - getList().size() + 1);
                            } else
                                TakePhotoActivity.startActivityForSiglePhoto(
                                        (Activity) context, requestCode, false);
                        } else {
                            List<IBanner> imgs = new ArrayList<IBanner>();

                            for (IBanner temp : getList()) {
                                if (!TextUtils.isEmpty(temp.getUrl())) {
                                    imgs.add(temp);
                                }
                            }
                            if (imgs.size() == list.size()) {
                                PhotoViewActivity.startActivity(context,
                                        position, imgs);
                            } else {
                                PhotoViewActivity.startActivity(context,
                                        position - 1, imgs);
                            }

                        }
                    }
                });
            }
        };
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.item_image_add;
    }

}
